Ext.define('QMS.view.contextMenu', {
	extend: 'Ext.menu.Menu',
	alias: 'widget.contextmenu',
	items: 
	[
		{ 
			text: 'Add',
			handler: function() 
			{
				QMS.app.fireEvent("contextmenuadd", this.up('contextmenu').currentModelName);
			} 
		},
		{
			text: 'Delete',
			handler: function()
			{
				QMS.app.fireEvent("contextmenudelete", this.up('contextmenu').currentModelName);
			}
		}
	]
})

Ext.define('QMS.view.OrgTree', {
	extend: 'Ext.tree.Panel',
	alias: 'widget.orgtree',
	store: 'OrgTree',
	rootVisible: false,
	id: 'orgtree',
	
	initComponent: function() {

		this.callParent(arguments);
	},

	// dockedItems: [{
	// 	xtype: 'button',
	// 	text: 'refresh',
	// 	handler: function () {
	// 		QMS.app.fireEvent("debugreloadtree");
	// 	},
	// }],

	contextMenu: new QMS.view.contextMenu(),

	listeners: {
		itemcontextmenu: function(OrgTree, record, item, index, e, eOpts)
		{
			e.stopEvent();

			if (index == 0) {
				// Hide "Delete" button
				this.contextMenu.items.items[1].hide();
			} else {
				// Show "Delete" button
				this.contextMenu.items.items[1].show();
			}
			this.contextMenu.currentModelName = record.internalId;
			this.contextMenu.showAt(e.getXY());
		},
		containercontextmenu: function(OrgTree, e, eOpts)
		{
			e.stopEvent();
			
			// Hide "Delete" button
			this.contextMenu.items.items[1].hide();
			this.contextMenu.currentModelName = "ORG-1";
			this.contextMenu.showAt(e.getXY());
		},
		beforeitemclick: function(OrgTree, td, cellIndex, record, tr, rowIndex, e, eOpts)
		{
			
		},
	},

});